package com.dha.dao.impl;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.dha.custom.XemThoiKhoaBieuCustom;
import com.dha.dao.SinhVienDAO;

@WebService(targetNamespace = "SinhVienService")
public class SinhVienDAOImpl implements SinhVienDAO {
	private SessionFactory sessionFactory = new Configuration().configure()
			.buildSessionFactory();
	private Session session;

	@Override
	@WebMethod(operationName = "SinhVienDangNhap")
	public int sinhVienDangNhap(
			@WebParam(name = "maSinhVien") String maSinhVien,
			@WebParam(name = "matKhau") String matKhau) {
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();

			String sql = "from SinhVienEntity where maSinhVien = ? and matKhau = ?";

			Query query = session.createQuery(sql);
			query.setParameter(0, maSinhVien);
			query.setParameter(1, matKhau);
			// tra ve so dong dc select, tra ve 1 la ok, khac 1 la sai, -1 la co
			// exception
			return query.list().size();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return -1;
		} finally {
			session.close();
		}
	}

	@Override
	@WebMethod(operationName = "ChaoSinhVien")
	public String chaoSinhVien(@WebParam(name = "MaSinhVien") String maSinhVien) {
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();

			String sql = "select tenSinhVien from SinhVienEntity where maSinhVien = ?";

			Query query = session.createQuery(sql);
			query.setParameter(0, maSinhVien);

			String ketQua = query.list().get(0).toString();

			return ketQua;
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return null;
		} finally {
			session.close();
		}
	}

	

}
